﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XuanXiang.Infrastructure.Attr;
using XuanXiang.Infrastructure.Dto.Request;
using XuanXiang.Infrastructure.Dto.Request.Merchant;
using XuanXiang.Infrastructure.Dto.Response;
using XuanXiang.Infrastructure.Dto.Response.Goods;
using XuanXiang.Infrastructure.Dto.Response.Merchant;
using XuanXiang.Infrastructure.Dto.Response.Orders;
using XuanXiang.Infrastructure.Dto.Response.Promote;
using XuanXiang.Infrastructure.EFCore.MySql;
using XuanXiang.Infrastructure.IOC;
using XuanXiang.Infrastructure.Tools;
using XuanXiang.Service.Interface;

namespace XuanXiang.Service.Instance
{
    [Inject_]
    public class Orders_Service : Base_Service, I_Orders_Service
    {

        private readonly Merchant_IOC _merchant_IOC;
        private readonly Supplier_IOC _supplier_IOC;
        private readonly Goods_IOC _goods_IOC;
        private readonly Sys_IOC _sys_IOC;

        public Orders_Service(
            Supplier_IOC supplier_IOC,
            Merchant_IOC merchant_IOC,
            Goods_IOC goods_IOC,
            Sys_IOC sys_IOC)
        {

            _merchant_IOC = merchant_IOC;
            _supplier_IOC = supplier_IOC;
            _goods_IOC = goods_IOC;
            _sys_IOC = sys_IOC;
        }

        /// <summary>
        /// 获取活动列表
        /// </summary>
        /// <param name="fenye_Dto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Api_Result_Dto> Get_Orders(Fenye_Dto dto)
        {
            var data = await _merchant_IOC._orders_EFCore
                .GetAll(out int total, dto.page, dto.limit,
                false, o => o.OcancelTime)
                .Select(d => new Orders_Dto()
                {
                    id = d.Oid,
                    goodsCount = d.Ototal,
                    goodsName = d.OgoodsNames,
                    goodsTypeCount = d.OgoodsTypeCount,
                    isCancel = d.OisCancel,
                    isComplete = d.OisComplete,
                    isReceive = d.OisReceive,
                    isSend = d.OisSend,
                    m_id = d.Omid,
                    s_id = d.Osid,
                    money = d.Omoney,
                    isAccept= d.OisAccept,
                    time = d.OcreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss")
                })
                .ToListAsync();


            //获取所有的商户
            var merchant = _merchant_IOC._merchant_EFCore.GetAll().ToList();
            //获取供应商
            var supplier = _supplier_IOC._supplier_EFCore.GetAll().ToList();
            

            data.ForEach(d =>
            {
                d.merchant = merchant.Where(c => c.MId == d.m_id).Select(c => c.MName).SingleOrDefault();
                d.supplier = supplier.Where(c => c.Sid == d.s_id).Select(c => c.Sname).SingleOrDefault();
            });
            data = data.Where(d =>
    (d.goodsName != null && d.goodsName.Contains(dto.key!)) ||
    (d.merchant != null && d.merchant.Contains(dto.key!)) ||
    (d.supplier != null && d.supplier.Contains(dto.key!)))
                .ToList() ?? data;
            return Get_Result(0, "ok", new
            {
                data = data,
                total = total,
            });
        }

        /// <summary>
        /// 获取订单的商品详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Api_Result_Dto> Get_Orders_Goods_Details(string? id)
        {
            //获取当前订单的所有商品id
            var orders = _merchant_IOC._order_Details_EFCore
                .GetAll(d => d.OdOid == id).Select(d => new
                {
                    gid = d.OdGid,
                    number = d.OdNumber,
                    money = d.OdMoney
                }).ToList();
            var g_id = orders.Select(d => d.gid).ToList();

            var data = await _goods_IOC._goods_EFCore
                 .GetAll(d => g_id.Contains(d.Gid))
                 .Select(d => new GoodsDetails_Dto()
                 {
                     id = d.Gid,
                     photo = d.Gcover,
                     name = d.Gname,
                     tId = d.Gtype,
                     s_id = d.GSid,
                     price = d.GisPromote==1?d.GpromotePrice:d.Gprice,

                 })
                 .ToListAsync();

            var types = _sys_IOC._sys_Dictionary_EFCore.GetAll().ToList();
            var supplier = _supplier_IOC._supplier_EFCore.GetAll().ToList();

            data.ForEach(d =>
            {
                var o = orders.Where(c => c.gid == d.id).SingleOrDefault();
                d.number = o.number;
                d.money = o.money;
                d.supplierName = supplier.Where(c => c.Sid == d.s_id).Select(c => c.Sname).SingleOrDefault();
                d.typeName = types.Where(c => c.Did == d.tId).Select(c => c.Dname).SingleOrDefault();
            });



            return Get_Result(0, "ok", data);

        }
    }
}
